# cython: language_level=3
import utils

"""

表达式工具类

"""


def get_result_from_expression(expression:str,feature_name:str,data_dict:dict,rule_name:str) -> bool:
    """
    计算结果
    :param expression:
    :param feature_name:
    :param data_dict:
    :return:
    """
    feature_name_list = feature_name.split(',')
    for name in feature_name_list:
        if 'none' == data_dict.get(name, 'none'):
            return False # 如果没有这个值，不过这个规则
    final_expression = expression
    for index in range(len(feature_name_list)):
        v = data_dict.get(feature_name_list[index])
        final_expression = final_expression.replace('$'+str(index+1),str(v))
    utils.get_logger().info(f'rule_name->{rule_name}|feature_name->{feature_name}|expression->{expression}|final_expression->{final_expression}')
    result = eval(final_expression)
    return result


if __name__ == '__main__':
    free_features = {
        'age':66,
        'locGpsCountryIsMexico' : 1,
        'userHaveNoRepayOrderCnt' : 0,
        'userPhoneHaveOrder' : 0,
        'userOCRIDCradHaveOrder' : 0,
        'smsRepaysuccessMoneyAvg60D' : 400,
        'loanAppCnt30D' : 5,
        'smsOverdueRemindSmsCnt3D' : 2
    }

    exp1 = '$1 < 20 or $1 > 55'
    exp2 = '$1 == 0'
    exp3 = '$1 > 1'
    exp4 = '$1 > 1'
    exp5 = '$1 > 1'
    exp6 = '$1 <= 375 and $2 > 6 and $3 < 3'
    exp7 = '($1 > 0 and $1 < 18) or $1 > 65'

    r = get_result_from_expression(exp1,'age',free_features,'m101')
    utils.get_logger().info(r)
    r = get_result_from_expression(exp7,'age',free_features,'m101')
    utils.get_logger().info(r)
    r = get_result_from_expression(exp6,'smsRepaysuccessMoneyAvg60D,loanAppCnt30D,smsOverdueRemindSmsCnt3D',free_features,'m102')
    utils.get_logger().info(r)
    r = get_result_from_expression(exp6,'smsRepaysuccessMoneyAvg60D,loanAppCnt30D,smsOverdueRemindSmsCnt3D,abc',free_features,'m103')
    utils.get_logger().info(r)
